package com.ruoyi.applet.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.applet.mapper.MiniLeagueMapper;
import com.ruoyi.applet.domain.MiniLeague;
import com.ruoyi.applet.service.IMiniLeagueService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 加盟商Service业务层处理
 *
 * @author liucan
 * @date 2024-03-25
 */
@Transactional
@Service
public class MiniLeagueServiceImpl implements IMiniLeagueService {
    @Resource
    private MiniLeagueMapper miniLeagueMapper;

    /**
     * 查询加盟商
     *
     * @param id 加盟商主键
     * @return 加盟商
     */
    @Override
    public MiniLeague selectMiniLeagueById(Long id) {
        return miniLeagueMapper.selectMiniLeagueById(id);
    }

    /**
     * 查询加盟商列表
     *
     * @param miniLeague 加盟商
     * @return 加盟商
     */
    @Override
    public List<MiniLeague> selectMiniLeagueList(MiniLeague miniLeague) {
        return miniLeagueMapper.selectMiniLeagueList(miniLeague);
    }

    /**
     * 登录加盟商
     */
    @Override
    public List<MiniLeague> login(MiniLeague miniLeague){
        return miniLeagueMapper.login(miniLeague);
    }

    /**
     * 新增加盟商
     *
     * @param miniLeague 加盟商
     * @return 结果
     */
    @Override
    public AjaxResult insertMiniLeague(MiniLeague miniLeague) {
        //初始化对象，根据手机号和经销商名称查重
        MiniLeague miniLeague1 = new MiniLeague();
        miniLeague1.setLeNumber(miniLeague.getLeNumber());//手机号
        miniLeague1.setLeName(miniLeague.getLeName());//经销商名称
        List<MiniLeague> miniLeagues = miniLeagueMapper.selectMiniLeagueList(miniLeague1);
        if (miniLeagues.size() > 0){
            return AjaxResult.error("该经销商名称或手机号码已有存档，不可重复申请!");
        }
        miniLeague.setCreationTime(DateUtils.getTime());//创建时间
        int i = miniLeagueMapper.insertMiniLeague(miniLeague);
        if (i < 1){
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    /**
     * 修改加盟商
     *
     * @param miniLeague 加盟商
     * @return 结果
     */
    @Override
    public int updateMiniLeague(MiniLeague miniLeague) {
        //判断是否是启用操作
        if (miniLeague.getLeStatus() == 1 && miniLeague.getLeTime() == null){//1启用
            miniLeague.setLeTime(DateUtils.getTime());//审批时间
        }
        return miniLeagueMapper.updateMiniLeague(miniLeague);
    }

    /**
     * 批量删除加盟商
     *
     * @param ids 需要删除的加盟商主键
     * @return 结果
     */
    @Override
    public int deleteMiniLeagueByIds(Long[] ids) {
        return miniLeagueMapper.deleteMiniLeagueByIds(ids);
    }

    /**
     * 删除加盟商信息
     *
     * @param id 加盟商主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMiniLeagueById(Long id) {
        return miniLeagueMapper.deleteMiniLeagueById(id);
    }
}
